package codeTop;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class lcr159 {
    public int[] inventoryManagement(int[] stock, int cnt) {
        if (cnt == 0) {
            return new int[]{};
        }
        quickSelect(stock,0, stock.length-1,cnt);
        return Arrays.copyOfRange(stock,0,cnt);
    }
    public void quickSelect(int[] nums,int l,int r,int cnt){
        int partition = partition(nums, l, r);
        if (partition + 1 == cnt) {
            return;
        }
        if (partition + 1 < cnt) {
            quickSelect(nums,partition+1,r,cnt);
        }else {
            quickSelect(nums,l,partition-1,cnt);
        }
    }
    public int partition(int[] nums,int l ,int r){
        int pivot = nums[l];
        while (l < r) {
            while (l<r&&nums[r]>=pivot) r--;
            nums[l] = nums[r];
            while (l<r&&nums[l]<=pivot) l++;
            nums[r] = nums[l];
        }
        nums[l] = pivot;
        return l;
    }
}
